package com.atguigu.edu2021.mock.factory;

import com.atguigu.edu2021.mock.bean.BaseProvince;
import com.atguigu.edu2021.mock.bean.UserInfo;
import com.atguigu.edu2021.mock.log.AppCommon;
import com.atguigu.edu2021.mock.service.BaseProvinceService;
import com.atguigu.edu2021.mock.service.OrderInfoService;
import com.atguigu.edu2021.mock.service.UserInfoService;
import com.atguigu.edu2021.mock.stage.Stage;

import lombok.Data;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.*;

@Component("userStageChain")
@Scope("prototype")
@Data
public class UserStageChain implements  Runnable{




    @Autowired
    UserInfoService userInfoService;

    @Autowired
    BaseProvinceService baseProvinceService;

    @Autowired
    OrderInfoService orderInfoService;

    @Setter
    List<Stage> visitStages =new LinkedList<>();

    @Setter
    List<Stage> studyStages= new  LinkedList<>();

    @Setter
    Date  startDatetime;





    public UserStageChain addVisitStage(Stage stage){
        visitStages.add(stage);
        return  this;
    }

    public UserStageChain addStudyStage(Stage stage){
        studyStages.add(stage);
        return  this;
    }

//    AppCommon common;

    public void initAppCommonInfo(){
        AppCommon appCommon =   AppCommon.build() ;
        UserInfo userInfo = UserSession.getUserInfo();
        Long id = userInfo.getId();
        BaseProvince baseProvince = baseProvinceService.getOneByRandom();
        appCommon.setAr(baseProvince.getId().toString());
        appCommon.setUid(id.toString());
        appCommon.setSid(UserSession.getSessionId());
        UserSession.setAppCommon(appCommon);
    }


    public void initSessionId(){
        String sessionId = UUID.randomUUID().toString();
        UserSession.setSessionId(sessionId);
    }


    public void   initUserInfo(){

        UserInfo  userInfo = userInfoService.getOneByRandom();
        UserSession.setUserInfo(userInfo) ;

    }

    public  void initTime(){
        UserSession.setCurDateTime(startDatetime);
    }


    @Override
    public void run() {
        initSessionId();
        initUserInfo();
        initAppCommonInfo();
        initTime();

        if( checkifStudy()){
            UserSession.setPurpose(UserSession.PURPOSE_STUDY);
            handleStages(studyStages);

        }else{
            UserSession.setPurpose(UserSession.PURPOSE_VISIT);
            handleStages(visitStages);
        }



    }


    private  void handleStages( List<? extends  Stage> stages){
        for (int i = 0; i < stages.size(); i++) {
            if(i<stages.size()-1){
                UserSession.setNextStageInfo( stages.get(i+1).getClass()  );
            }
            boolean canContinue =  stages.get(i).handle(); 
            if(!canContinue){
                break;
            }
        }
    }




    public boolean checkifStudy(){
         return userInfoService.checkifStudy(UserSession.getUserInfo().getId());
    }
}
